#ifndef __MUTEX_HPP__
#define __MUTEX_HPP__

#include <pthread.h>
#include "logger.hpp"

struct Mutex {
	Mutex() {
		pthread_mutex_init( &mutex, NULL );
	}
	virtual ~Mutex() {
		pthread_mutex_destroy( &mutex );
		void lock() {
			pthread_mutex_lock( &mutex );
		}
		void unlock() {
			pthread_mutex_unlock( &mutex );
		}

private:
		Mutex(const Mutex&);  /* prevent copying */
		Mutex& operator=(const Mutex&);
		pthread_mutex_t mutex;
	};

	class MutexLocker
	{
	public:
		MutexLocker(Mutex *mutex):
			_mutex(mutex) {
			_mutex->lock();
		}
		virtual ~MutexLocker() {
			_mutex->unlock();
		}
	private:
		Mutex *_mutex;
	};

#endif
